package org.zjsru.order.web;

import org.zjsru.order.pojo.Order;
import org.zjsru.order.pojo.User;
import org.zjsru.order.service.OrderService;
import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.zjsru.order.service.OrderServiceFeign;

@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private OrderService orderService;
   // @Autowired
   // @Autowired
   // NacosServiceDiscovery serviceDiscovery ;
    @Autowired
   OrderServiceFeign ordersServiceFeign;
    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) throws NacosException {
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        /*ServiceInstance instance = serviceDiscovery.getInstances("user-service").get(0);
        String url = instance.getHost() + ":" + instance.getPort() ;
        User user = restTemplate.getForObject("http://" + url + "/user/" + order.getUserId(), User.class);*/
        //User user = restTemplate.getForObject("http://user-service/user/" + order.getUserId(), User.class);
        User user = ordersServiceFeign.queryUserById(order.getUserId());
        order.setUser(user);
        return order;
    }
}
